package valuecomusa.fijibus.eticket.dialog;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import valuecomusa.fijibus.eticket.Declarations;
import valuecomusa.fijibus.eticket.Utilities;
import valuecomusa.fijibus.eticket.WorkingSession;
import valuecomusa.fijibus.eticket.db.shorthaul.BusinessDBHandler;
import valuecomusa.fijibus.eticket.shorthaul.R;

import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Camel
 * Date: 8/20/13
 * Time: 11:27 AM
 * To change this template use File | Settings | File Templates.
 */
public class CheckInV2_Stage_SelectMore extends Activity
{
	private BusinessDBHandler db;
	private Object[] stageButtonActive;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_in_v2_stage_select_more);
		//---
		db = new BusinessDBHandler(CheckInV2_Stage_SelectMore.this);
		//--
		CreateAndRegisterStageButtons();
		RegisterSubmitCancelButtons();
	}

	private void CreateAndRegisterStageButtons()
	{
		int stageCount = db.getStageCountOfRoute(WorkingSession.getInstance().journalRoute.ISN);
		stageButtonActive = new Object[stageCount];
		Arrays.fill(stageButtonActive, null);
		//--
		LinearLayout container = (LinearLayout)findViewById(R.id.check_in_v2_stage_select_more_container);
		//--
		int stagesPerRow = 3;
		int rowCount = stageCount / stagesPerRow + (stageCount % stagesPerRow > 1? 1: 0);

		Resources res = this.getResources();
		for(int r=0;r<rowCount;r++)
		{
			LinearLayout layout = new LinearLayout(this);
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,  //width
					LinearLayout.LayoutParams.WRAP_CONTENT); //height
			layout.setOrientation(LinearLayout.HORIZONTAL);
			param.setMargins(0, getDIPFromPixel(20), 0, 0);
			param.gravity = Gravity.CENTER_HORIZONTAL;
			layout.setLayoutParams(param);
			//--
			int c = 0;
			for(c=0; c<stagesPerRow && r*stagesPerRow+c < stageCount; c++)
			{
				Button stage = new Button(CheckInV2_Stage_SelectMore.this);
				param = new LinearLayout.LayoutParams(
						getDIPFromPixel(120), //width
						getDIPFromPixel(60));//height
				param.setMargins((c == 0? 0: getDIPFromPixel(25)), 0, 0, 0);
				//--
				stage.setLayoutParams(param);
				stage.setText(String.valueOf(r*stagesPerRow+c+1) + " Stage" + (r*stagesPerRow+c+1==1?"":"s"));
				stage.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
				stage.setBackground(res.getDrawable(R.drawable.stage_text_button_states));
				stage.setOnClickListener(onStageButtonClick);
				stage.setTag(r*stagesPerRow+c);
				//--
				layout.addView(stage);
			}
			//--
			container.addView(layout);
		}//end of for
	}

	private int getDIPFromPixel(int value)
	{
		int x = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP,
				value,
				getResources().getDisplayMetrics());
		return x;
	}

	View.OnClickListener onStageButtonClick = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			Button button = (Button) v;
			String text = button.getText().toString();
			int idx = -1;
			idx = Integer.parseInt(String.valueOf(button.getTag()));
			//--
			if(stageButtonActive[idx] == null)
			{
				for(int i=0;i<stageButtonActive.length;i++)
				{
					if(stageButtonActive[i] != null) //current selected
					{
						((Button)stageButtonActive[i]).setBackground(
								CheckInV2_Stage_SelectMore.this.getResources().getDrawable(
										R.drawable.stage_text_button_states
								)
						);
						stageButtonActive[i] = null;
					}
				}
				button.setBackground(
						CheckInV2_Stage_SelectMore.this.getResources().getDrawable(
								R.drawable.stage_text_button_states2
						)
				);
				stageButtonActive[idx] = button;

			}
		}
	};

	private void RegisterSubmitCancelButtons()
	{
		Button cancel = (Button)findViewById(R.id.check_in_v2_stage_select_more_cancel);
		cancel.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setResult(RESULT_CANCELED);
				finish();
			}
		});
		//--
		Button submit = (Button)findViewById(R.id.check_in_v2_stage_select_more_submit);
		submit.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				int selectStage = 0;
				for(int i=0;i<stageButtonActive.length;i++)
				{
					if(stageButtonActive[i] != null)
					{
						selectStage = i+1;
						break;
					}
				}

				Intent data = new Intent();
				data.putExtra("SELECTED_STAGE", selectStage);

				setResult(RESULT_OK, data);
				finish();
			}
		});
	}
}